<template>
  <div class="app-container">
    <!-- 分页查询 -->
    <el-form
      ref="queryParams"
      :model="queryParams"
      size="small"
      label-suffix=":"
      label-width="150px"
    >
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item :label="$t('depotLang.depot')" prop="depotId">
            <el-select
              v-model="queryParams.depotId"
              :placeholder="$t('pleaseSelect')"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotOptionList"
                :key="item.value"
                :value="item.value"
                :label="item.title"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item
            :label="$t('materialsLang.packagingMaterialName')"
            prop="packName"
          >
            <el-input
              v-model="queryParams.packName"
              :placeholder="$t('pleaseEnter')"
              style="width: 200px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="1.5">
          <el-form-item
            :label="$t('materialsLang.packagingMaterial')"
            prop="packMaterial"
          >
            <el-select
              v-model="queryParams.packMaterial"
              :placeholder="$t('pleaseSelect')"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotPackMaterialOptions"
                :key="item.value"
                :value="item.value"
                :label="$t('statusLang.' + item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-form-item
            prop="packType"
            :label="$t('materialsLang.packagingMaterialType')"
          >
            <el-select
              v-model="queryParams.packType"
              :placeholder="$t('pleaseSelect')"
              clearable
              style="width: 200px"
            >
              <el-option
                v-for="item in depotPackTypeOptions"
                :key="item.value"
                :value="item.value"
                :label="$t('statusLang.' + item.title)"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="handleQuery"
          >
            {{ $t("query") }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="el-icon-refresh-left"
            @click="handleReset"
          >
            {{ $t("reset") }}
          </el-button>
        </el-col>
      </el-row>
    </el-form>
    <!-- 操作栏 -->
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button type="primary" size="small" @click="handleAdd">
          {{ $t("add") }}
        </el-button>
      </el-col>
    </el-row>
    <!-- 数据列表 -->
    <el-table
      ref="table"
      v-loading="loading"
      :data="tableData"
      border
      class="mt-10"
    >
      <el-table-column
        :label="$t('tableIndex')"
        type="index"
        :index="indexAdd"
        align="center"
        width="80"
      />
      <el-table-column
        :label="$t('depotLang.depot')"
        prop="depotCodeAndName"
        align="center"
        min-width="150"
      />
      <el-table-column
        :label="$t('materialsLang.packagingMaterialName')"
        prop="packName"
        min-width="100"
        align="center"
      />
      <el-table-column
        :label="$t('materialsLang.packagingMaterial')"
        prop="packMaterial"
        align="center"
        min-width="100"
      >
        <template slot-scope="scope">
          <span>{{
            $t(
              "statusLang." +
                getTitleByValue(
                  depotPackMaterialOptions,
                  scope.row.packMaterial
                )
            )
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('materialsLang.packagingMaterialType')"
        prop="packType"
        align="center"
        min-width="100"
      >
        <template slot-scope="scope">
          <span>{{
            $t(
              "statusLang." +
                getTitleByValue(depotPackTypeOptions, scope.row.packType)
            )
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('harvestLang.customerName')"
        align="center"
        min-width="220"
        prop="customerCodeAndName"
      />
      <el-table-column
        :label="$t('unitLang.weight') + '/' + $t('unitLang.size')"
        prop="size"
        align="left"
        min-width="150"
      >
        <template slot-scope="scope">
          <p>
            {{ $t("unitLang.weight") + " : "
            }}<span>{{ scope.row.packWeight }}</span>
          </p>
          <p>
            {{ $t("unitLang.size") + " : "
            }}<span>{{ scope.row.packSize }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('statusLang.inLibrary') + $t('number')"
        prop="packNum"
        align="center"
        min-width="80"
      />
      <el-table-column
        :label="$t('statusLang.usable') + $t('number')"
        prop="canUseNum"
        align="center"
        min-width="80"
      />
      <el-table-column
        :label="$t('statusLang.packaged') + $t('number')"
        prop="packagedNum"
        align="center"
        min-width="80"
      />
      <el-table-column
        :label="$t('goodsSKULang.storageLocation')"
        prop="locationCode"
        align="center"
        min-width="80"
      />
      <el-table-column
        :label="$t('operation')"
        width="100"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">
            {{ $t("edit") }}
          </el-button>
          <el-button type="text" @click="handleDelete(scope.row)">
            {{ $t("delete") }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :total="total"
        layoout="total,prev,pager,next"
        :current-page="queryParams.pageNo"
        :page-size="queryParams.pageSize"
        @current-change="pageNoChange"
      />
    </el-row>
    <!-- 表单 -->
    <info-form
      v-model="infoParams.id"
      :visible.sync="infoVisiable"
      :operation="infoParams.operation"
      :title="infoTitle"
      @close="infoClose"
    />
  </div>
</template>

<script>
import InfoForm from '../components/info-form.vue'
import { getDepotListAllStatus } from '@/api/business/depotManage/depot'
import { depotPackMaterialDict, depotPackTypeDict } from '@/utils/dictData'
import { getDictOptionList, getTitleByValue } from '@/utils/dictUtils'
import {
  getDepotPackPageList,
  deleteDepotPack
} from '@/api/business/packagingMaterials/material'
export default {
  name: '',
  components: { InfoForm },
  data() {
    return {
      total: 0,
      loading: false,
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        depotId: null,
        packMaterial: null,
        packName: null,
        packType: null
      },
      infoParams: {
        id: null,
        operation: null
      },
      infoVisiable: false,
      infoTitle: '',
      tableData: [],
      data: [],
      dialog: false,
      depotOptionList: [], // 仓库下拉
      getTitleByValue: getTitleByValue,
      depotPackTypeOptions: getDictOptionList(depotPackTypeDict), // 包材类型下拉
      depotPackMaterialOptions: getDictOptionList(depotPackMaterialDict) // 包材材质下拉
    }
  },
  created() {
    this.getDepotListAllStatus()
    this.getDepotPackPageList()
  },
  methods: {
    // 查询
    handleQuery() {
      this.queryParams.pageNo = 1
      this.loading = true
      this.getDepotPackPageList()
      this.loading = false
    },
    // 重置
    handleReset() {
      this.$refs.queryParams.resetFields()
    },
    // 新增
    handleAdd() {
      this.infoParams.operation = 1
      this.infoTitle = this.$t('add')
      this.infoVisiable = true
    },
    // 编辑
    handleEdit(row) {
      this.infoParams.operation = 2
      this.infoTitle = this.$t('edit')
      this.infoParams.id = row.packId
      this.$nextTick(() => {
        this.infoVisiable = true
      }, 400)
    },
    // 删除
    async handleDelete(row) {
      const flag = await this.$confirm(
        this.$t('delete') + '?',
        this.$t('hint'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).catch((err) => err)
      if (flag === 'cancel') return
      const res = await deleteDepotPack({
        packId: row.packId
      })
      if (res && res.returnCode === 10000) {
        this.$message({
          type: 'success',
          message: this.$t('handleSuccess')
        })
        this.getDepotPackPageList()
      }
    },
    // 获取仓库下拉
    async getDepotListAllStatus() {
      const res = await getDepotListAllStatus()
      if (!res) return
      this.depotOptionList = res.data || []
    },
    // 获取包材列表
    async getDepotPackPageList() {
      const { data } = await getDepotPackPageList({
        ...this.queryParams
      })
      console.log(data, '包材')
      this.tableData = data.packPageDTOList
      this.total = data.totalRowNum
    },
    pageNoChange(newPage) {
      this.queryParams.pageNo = newPage
      this.getDepotPackPageList()
    },
    indexAdd(index) {
      // console.log(index)
      const page = this.queryParams.pageNo // 当前页码
      const pagesize = this.queryParams.pageSize // 每页条数
      return index + 1 + (page - 1) * pagesize
    },
    infoClose() {
      this.infoVisiable = false
      this.getDepotPackPageList()
      setTimeout(() => {
        this.infoParams = {
          id: null,
          operation: 0
        }
      }, 300)
    }
  }
}
</script>

<style></style>
